package com.hspedu.innerclass;

/**
 * 演示匿名内部类
 */
public class AnonymousInnerClass {
    public static void main(String[] args) {
        Outer04 outer04 = new Outer04();
        outer04.method();
    }
}


class Outer04 {//外部类
    private int n1 = 10;

    public void method() {
        //基于接口的匿名内部类
        //传统方法
        //Tiger tiger = new Tiger();
        //tiger.cry();
        //编译类型：A
        //运行类型：匿名内部类 Outer04$1
        /*
            底层会分配类名 Outer04$1
            class Outer04$1 implements A {
                @Override
                public void cry() {
                    System.out.println("老虎叫");
                }
            }
         */
        //jdk 底层在创建匿名内部类 Outer04$1，立即创建了 Outer04$1 实例，并且把地址返回给 tiger
        A tiger = new A() {
            @Override
            public void cry() {
                System.out.println("老虎叫");
            }
        };
        System.out.println("tiger的运行类型是：" + tiger.getClass());
        tiger.cry();
        tiger.cry();
        tiger.cry();


        //基于类的匿名内部类
        //1.编译类型 Father
        //2.运行类型 Outer04$2
        /*
            class Outer04$2 extents Father {
                @Override
                public void test() {
                    System.out.println("匿名内部类重写了text()");
                }
            }
         */
        Father father = new Father("jack") {
            @Override
            public void test() {
                System.out.println("匿名内部类重写了text()");
            }
        };
        System.out.println("father对象的运行类型=" + father.getClass());//Outer04$2
        father.test();


        //基于抽象类的匿名内部类
        Animal animal = new Animal() {
            @Override
            void eat() {
                System.out.println("吃东西吃东西吃东西");
            }
        };
        animal.eat();
    }
}

interface A {
    void cry();
}

//class Tiger implements A {
//
//    @Override
//    public void cry() {
//        System.out.println("老虎叫");
//    }
//}
//
//class Dog implements A {
//
//    @Override
//    public void cry() {
//        System.out.println("小狗叫");
//    }
//}

class Father {
    public Father(String name) {

    }

    public void test() {
    }
}

abstract class Animal {
    abstract void eat();
}